j3deditor.bin.util
Class ByteConverter

java.lang.Object
  extended by j3deditor.bin.util.ByteConverter

public class ByteConverter
extends java.lang.Object

Tool for converting numbers to byte arrays and vice versa.

Author:
Risto Seene

Constructor Summary
ByteConverter()
           
 
Method Summary
static byte[] getBytes(float nr)
          Encodes the given float into a sequence of bytes.
static byte[] getBytes(int nr)
          Encodes the given int into a sequence of bytes.
static byte[] getBytes(java.lang.String s)
          Encodes the given String into a sequence of bytes using the "utf-8" charset.
static boolean isFlagSet(int allFlags, int theFlag)
          Tests if the given flag is present.
static java.util.BitSet toBits(byte[] bytes)
          Returns the bits of the given array of bytes.
static byte[] toBytes(java.util.BitSet bits)
          Returns the given bits as the array of bytes.
static float toFloat(byte[] bytes)
          Decodes the specified array of bytes to float.
static int toInt(byte[] bytes)
          Decodes the specified array of bytes to int.
static java.lang.String toString(byte[] bytes)
          Decodes the specified array of bytes to String using "utf-8" charset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteConverter

public ByteConverter()
Method Detail

getBytes

public static byte[] getBytes(int nr)
Encodes the given int into a sequence of bytes.

Parameters:
nr - integer to be encoded
Returns:
Returns bytes of the given int

getBytes

public static byte[] getBytes(float nr)
Encodes the given float into a sequence of bytes.

Parameters:
nr - float to be encoded
Returns:
Returns bytes of the given float

getBytes

public static byte[] getBytes(java.lang.String s)
Encodes the given String into a sequence of bytes using the "utf-8" charset.

Parameters:
s - string to be encoded
Returns:
Returns bytes of the given String

toInt

public static int toInt(byte[] bytes)
                 throws java.lang.Exception
Decodes the specified array of bytes to int.

Parameters:
bytes - byte array to convert
Returns:
Returns integer converted from bytes
Throws:
java.lang.Exception - IllegalArgumentException if the length of the given array is not equal to 4,
NullPointerException if the given array is null

toFloat

public static float toFloat(byte[] bytes)
                     throws java.lang.Exception
Decodes the specified array of bytes to float.

Parameters:
bytes - byte array to convert
Returns:
Returns float converted from bytes
Throws:
java.lang.Exception - IllegalArgumentException if the length of the given array is not equal to 4,
NullPointerException if the given array is null

toString

public static java.lang.String toString(byte[] bytes)
                                 throws java.lang.Exception
Decodes the specified array of bytes to String using "utf-8" charset.

Parameters:
bytes - array of bytes to be decoded
Returns:
Returns the decoded String
Throws:
java.lang.Exception - if unable to decode the given array

toBits

public static java.util.BitSet toBits(byte[] bytes)
Returns the bits of the given array of bytes.

Parameters:
bytes - bytes which bits will be returned
Returns:
Returns the bits of the given array of bytes

toBytes

public static byte[] toBytes(java.util.BitSet bits)
Returns the given bits as the array of bytes.

Parameters:
bits - bits that will be converted to an array of bytes
Returns:
Returns the given bits as the array of bytes

isFlagSet

public static boolean isFlagSet(int allFlags,
                                int theFlag)
Tests if the given flag is present.

Parameters:
allFlags - flags that are bitwise OR'ed together
theFlag - wanted flag
Returns:
Returns true if the given flag is set, otherwise false